﻿/**
* 作者：福州刘宝宝网络科技
* http://www.liubaobao.cn
* 2011年10月20日
**/
/****************************************String扩张程序**************************************/
String.prototype.endsWith = function (suffix) {
    return this.match(suffix + "$") == suffix;
};

String.prototype.startsWith = function (prefix) {
    return this.indexOf(prefix) === 0;
}

String.prototype.trim = function () {
    return this.replace(/(^[\\s]*)|([\\s]*$)/g, "");
}
String.prototype.lTrim = function () {
    return this.replace(/(^[\\s]*)/g, "");
}
String.prototype.rTrim = function () {
    return this.replace(/([\\s]*$)/g, "");
}
//去掉最后一个字节
String.prototype.removeLastChar = function () {
    return this.substr(0, this.length - 1);
}

String.prototype.isNullOrEmpty = function () {
    if (this == null || this == undefined || this == "") {
        return true;
    } else {
        if (this.trim() == "") {
            return true;
        }
        return false;
    }
}
String.prototype.removeAfter = function (spliter) {
    return this.split(spliter)[0];
};

//生成 GUID
function Guid() {
    var S4 = function () {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    };
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}

/* object to string */
/** 需要更多的测试 **/
function objToString(o) {
    var r = [], i, j = 0, len;
    if (o == null) {
        return "\"\"";
    }
    if (typeof o == 'string') {
        return '"' + o + '"';
    }
    if (typeof o == 'object') {
        if (!o.sort) {
            r[j++] = '{';
            for (i in o) {
                //r[j++] = '"';
                r[j++] = i;
                r[j++] = ":";
                //  r[j++] = '":';
                r[j++] = objToString(o[i]);
                r[j++] = ',';
            }
            //可能的空对象
            //r[r[j-1] == '{' ? j:j-1]='}';
            r[j - 1] = '}';
        } else {
            r[j++] = '[';
            for (i = 0, len = o.length; i < len; ++i) {
                r[j++] = objToString(o[i]);
                r[j++] = ',';
            }
            //可能的空数组
            r[len == 0 ? j : j - 1] = ']';
        }
        return r.join('');
    }
    return o.toString();
}